package com.badlogic.androidgames;

import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class SoundPoolTest extends Activity implements OnTouchListener {

	SoundPool soundPool;
	int explosionId = -1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		TextView textView = new TextView(this);
		textView.setOnTouchListener(this);
		setContentView(textView);
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
		
		try {
			
		    soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
			AssetManager assetManager = getAssets();
			AssetFileDescriptor descriptor = assetManager.openFd("explosion.ogg");		
			explosionId = soundPool.load(descriptor, 1);
			
		} catch (IOException e) {
			textView.setText("Couldn't load sound effect from asset, "
			+ e.getMessage());
			//Log.d("Couldn't load sound effect from asset", e.getMessage());
		}
		
	}
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		try{
			if (event.getAction() == MotionEvent.ACTION_UP) {
				if (explosionId != -1) {
				soundPool.play(explosionId, 1, 1, 0, 0, 1);				
				}
			}
		}
		catch (Exception e) {
			((TextView)v).setText("valore di explosionID eccezione, "  +event.getAction() );
			((TextView)v).setText("Couldn't load sound effect from asset eccezione, "
			+ e.getMessage());
			
		}
		
		return true;
		
	}

}
